package com.foreknow.elm.dao.impl;

import com.foreknow.elm.dao.CartDao;
import com.foreknow.elm.po.Business;
import com.foreknow.elm.po.Cart;
import com.foreknow.elm.po.Food;
import com.foreknow.elm.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CartDemoImpl implements CartDao {
    private Connection con;
    private PreparedStatement pst;
    private ResultSet rs;
    @Override
    public List<Cart> listCart(Cart cart) throws SQLException {
        List<Cart> list = new ArrayList<>();
        //StringBuffer 是一个类
        StringBuffer s = new StringBuffer("SELECT * FROM cart,business,food WHERE cart.foodId = food.foodId AND cart.businessId = business.businessId AND cart.userId = ?");
        if (cart.getBusinessId() != null){
            s.append("and businessId ="+cart.getBusinessId());
        }
        try {
            //Connection从ThreadLocal中获取
            con = DBUtil.getConnection();
            pst = con.prepareStatement(s.toString());//调用的时候要把StringBuffer转换成字符串

            //将？替换为具体的值
            pst.setString(1,cart.getUserId());
            //pst.setInt(2,cart.getBusinessId());

            //执行查询并将查询到的结果放到结果集
            rs = pst.executeQuery();
            //遍历结果集并将结果集中的每行数据保存到List集合中
            while (rs.next()){
                Food food = new Food();
                food.setFoodId(rs.getInt("foodId"));
                food.setFoodName(rs.getString("foodName"));
                food.setFoodExplain(rs.getString("foodExplain"));
                food.setFoodImg(rs.getString("foodImg"));
                food.setFoodPrice(rs.getDouble("foodPrice"));
                food.setBusinessId(rs.getInt("businessId"));
                food.setRemarks(rs.getString("remarks"));

                Business business = new Business();
                business.setBusinessId(rs.getInt("businessId"));
                business.setBusinessName(rs.getString("businessName"));
                business.setBusinessAddress(rs.getString("businessAddress"));
                business.setBusinessExplain(rs.getString("businessExplain"));
                business.setBusinessImg(rs.getString("businessImg"));
                business.setOrderTypeId(rs.getInt("orderTypeId"));
                business.setStarPrice(rs.getDouble("starPrice"));
                business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
                business.setRemarks(rs.getString("remarks"));

                Cart cart1 = new Cart();
                cart1.setCartId(rs.getInt("cartId"));
                cart1.setFoodId(rs.getInt("foodId"));
                cart1.setBusinessId(rs.getInt("businessId"));
                cart1.setUserId(rs.getString("userId"));
                cart1.setQuantity(rs.getInt("quantity"));

                cart1.setFood(food);
                cart1.setBusiness(business);

                list.add(cart1);
            }
        }finally {
            DBUtil.close(pst);
        }

        return list;
    }

    @Override
    public int saveCart(String userId, Integer businessId, Integer foodId) throws SQLException {
        int i = 0;
        String sql = "INSERT INTO cart(userId,businessId,foodId,quantity) VALUES(?,?,?,1)";
        try{
            //Connection从ThreadLocal中获取
            con = DBUtil.getConnection();
            //获取预处理对象
            pst = con.prepareStatement(sql);
            //将？替换为具体的值
            pst.setString(1,userId);
            pst.setInt(2,businessId);
            pst.setInt(3,foodId);



            i = pst.executeUpdate();

        }finally {
            DBUtil.close(pst);
        }
        return i;
    }

    @Override
    public int updateCart(String userId, Integer businessId, Integer foodId, Integer quantity) throws SQLException {
        int i = 0;
        String sql = "UPDATE cart SET quantity = ? WHERE foodId = ? and businessId = ? and userId = ?";
        try{
            //Connection从ThreadLocal中获取连接
            con = DBUtil.getConnection();
            //获取预处理对象
            pst = con.prepareStatement(sql);
            //将？替换为具体的值
            pst.setInt(1,quantity);
            pst.setInt(2,foodId);
            pst.setInt(3,businessId);
            pst.setString(4,userId);
            //执行修改操作
            i = pst.executeUpdate();

        }finally {
            //关闭资源
            DBUtil.close(pst);
        }
        return i;
    }

    @Override
    public int removeCart(String userId, Integer businessId, Integer foodId) throws SQLException {
        int i = 0;
        StringBuffer s = new StringBuffer("DELETE FROM cart WHERE userId = ? and businessId = ?");
        if (foodId != null){
            s.append(" and foodId ="+foodId);
        }

        try {
            //Connection从ThreadLocal中获取
            con = DBUtil.getConnection();
            pst = con.prepareStatement(s.toString());
            //将？替换为具体的值
            pst.setString(1,userId);
            pst.setInt(2,businessId);
            i = pst.executeUpdate();

        } finally {
            DBUtil.close(pst);
        }
        return i;
    }
}
